package com.example.fw;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public abstract class WebDriverHelperBase extends HelperBase {
		
	protected WebDriver driver;
	
	public WebDriverHelperBase(ApplicationManager manager){
		super(manager);
		this.driver = manager.getWebDriver();
	}
	
	public boolean isElementPresent(By by) {
		try {
			findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	protected void type(By locator, String text) {
		if (text != null) {
			clear(locator);
			findElement(locator).sendKeys(text);
		}		
	}

	protected WebElement findElement(By locator) {
		try{
			return driver.findElement(locator);
		}catch(Exception e){
			return null;
		}
		
	}

	private void clear(By locator) {
		findElement(locator).clear();
	}

	protected void click(By locator) {
		findElement(locator).click();
	}
	
	protected void selectByText(By locator, String text) {
		if (text != null) {
			new Select(findElement(locator)).selectByVisibleText(text);
		}		
	}
	
	protected void openUrl(String url){
		driver.get(manager.getProperty("baseUrl") + url);
	}
	
	protected void openAbsoluteUrl(String confirmationLink) {
		driver.get(confirmationLink);
	}
}
